//---------------------------------------------------------------------------
#pragma hdrstop
#include <vcl.h>
#include "CommData.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)

__fastcall IniFileOp::IniFileOp()
{
  asIniFilePath = ExtractFilePath(Application->ExeName) + "Set.ini";
}

bool IniFileOp::ReadIniFile(struct sysDbSetting *sysSetting)
{
  bool bDataExsit;
  if(!FileExists(asIniFilePath))
  {
    MessageBox(Application->Handle,
               "Short of Set.ini File",
               "Error",
               MB_ICONERROR);
    return false;
  }
  iniFile = new TIniFile(asIniFilePath);
  //-------Read [DataBase] Section-------
  if(!iniFile->SectionExists("DataBase")) return false;

  if(iniFile->ValueExists("DataBase","DB_Svr"))
    sysSetting->ServerSet.asSerIP =
      iniFile->ReadString("DataBase","DB_Svr","192.168.16.150");
  else
    return false;

  if(iniFile->ValueExists("DataBase","DB_User"))
    sysSetting->ServerSet.asUserName =
      iniFile->ReadString("DataBase","DB_User","sa");
  else
    return false;

  if(iniFile->ValueExists("DataBase","DB_PIN"))
    sysSetting->ServerSet.asUserPIN =
      iniFile->ReadString("DataBase","DB_PIN","");
  else
    return false;

  if(iniFile->ValueExists("DataBase","DB_Name"))
    sysSetting->ServerSet.asDB_Name =
      iniFile->ReadString("DataBase","DB_PIN","ComputerInfo");
  else
    return false;

  //-------Read [NetSeg] Section---------
  if(!iniFile->SectionExists("NetSeg")) return false;

  if(iniFile->ValueExists("NetSeg","SegmentCount"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","SegmentCount",0);
  else
    return false;

  if(iniFile->ValueExists("NetSeg","BaseAddress"))
    sysSetting->ServerSet.asDB_Name =
      iniFile->ReadString("NetSeg","BaseAddress","192.168.");

  if(iniFile->ValueExists("NetSeg","Segment1"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","Segment1",-1);

  if(iniFile->ValueExists("NetSeg","Segment2"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","Segment2",-1);

  if(iniFile->ValueExists("NetSeg","Segment3"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","Segment3",-1);

  if(iniFile->ValueExists("NetSeg","Segment4"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","Segment4",-1);

  if(iniFile->ValueExists("NetSeg","Segment5"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("NetSeg","Segment5",-1);

  //-------Read [Route] Section----------
  if(!iniFile->SectionExists("RouterRule")) return false;

  if(iniFile->ValueExists("RouterRule","RuleConut"))
    sysSetting->NetSegSet.iNetSegCount =
      iniFile->ReadInteger("RouterRule","RuleConut",0);
  else
    return false;

  if(iniFile->ValueExists("RouterRule","RuleNo1"))
    sysSetting->RouterRuleSet.iRule1 =
      iniFile->ReadInteger("RouterRule","RuleNo1",-1);

  if(iniFile->ValueExists("RouterRule","RuleNo2"))
    sysSetting->RouterRuleSet.iRule2 =
      iniFile->ReadInteger("RouterRule","RuleNo2",-1);

  if(iniFile->ValueExists("RouterRule","RuleNo3"))
    sysSetting->RouterRuleSet.iRule3 =
      iniFile->ReadInteger("RouterRule","RuleNo3",-1);

  if(iniFile->ValueExists("RouterRule","RuleNo4"))
    sysSetting->RouterRuleSet.iRule4 =
      iniFile->ReadInteger("RouterRule","RuleNo4",-1);

  if(iniFile->ValueExists("RouterRule","RuleNo5"))
    sysSetting->RouterRuleSet.iRule5 =
      iniFile->ReadInteger("RouterRule","RuleNo5",-1);

  //--------Set TableName Other----------
  sysSetting->SysTbName.asSrcTb = "Item";

  //------Read NetTableName Section------
  if(!iniFile->SectionExists("NetTable")) return false;

  if(iniFile->ValueExists("NetTable","Table1"))
    sysSetting->SysTbName.asNetTB1 =
      iniFile->ReadString("NetTable","Table1","");

  if(iniFile->ValueExists("NetTable","Table2"))
    sysSetting->SysTbName.asNetTB2 =
      iniFile->ReadString("NetTable","Table2","");

  if(iniFile->ValueExists("NetTable","Table3"))
    sysSetting->SysTbName.asNetTB3 =
      iniFile->ReadString("NetTable","Table3","");

  if(iniFile->ValueExists("NetTable","Table4"))
    sysSetting->SysTbName.asNetTB4 =
      iniFile->ReadString("NetTable","Table4","");

  if(iniFile->ValueExists("NetTable","Table5"))
    sysSetting->SysTbName.asNetTB5 =
      iniFile->ReadString("NetTable","Table5","");

  //---------Read OtherTable Name---------
  if(!iniFile->SectionExists("OtherTable")) return false;

  if(iniFile->ValueExists("OtherTable","CollectTable"))
    sysSetting->SysTbName.asCollectTb =
      iniFile->ReadString("OtherTable","CollectTable","");

  if(iniFile->ValueExists("OtherTable","CollectTable"))
    sysSetting->SysTbName.asCollectTb =
      iniFile->ReadString("OtherTable","CollectTable","");

  if(iniFile->ValueExists("OtherTable","UserTable"))
    sysSetting->SysTbName.asCollectTb =
      iniFile->ReadString("OtherTable","UserTable","");

  return true;
}
